package com.hr.service;

import com.hr.dao.ArchiveDAO;
import com.hr.dao.CompanyDAO;
import com.hr.dao.DeedDAO;
import com.hr.dao.DepartmentDAO;
import com.hr.model.Archive;
import com.hr.model.Company;
import com.hr.model.Department;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentService
{
    @Autowired
    DepartmentDAO departmentDAO;
    @Autowired
    ArchiveDAO archiveDAO;
    @Autowired
    CompanyDAO companyDAO;

    public Department getDepartmentByDepId(int depid)
    {

        return departmentDAO.getDepartmentByDepId(depid);
    }

    public List<Department> getDepartmentsByCId(int cid)
    {
        List<Department> departments=departmentDAO.getDepartmentsByCId(cid);
        for(Department department: departments)
        {
            List<Archive> archives=archiveDAO.getArchivesByDepId(department.getId());
            for(Archive archive:archives)
            {
                archive.cutPosition();
            }
            department.setArchives(archives);
        }
        return departments;
    }

    public boolean ifDepartmentBelong2Company(int depid, int cid)
    {
        Company company=companyDAO.getCompanyByCId(cid);

        boolean flag=false;
        for(Department department: company.getDepartments())
        {
            if(department.getId()==depid)
            {
                return true;
            }
        }
        return false;
    }

    public void insertDepartment(int cid,Department department)
    {
        departmentDAO.insertDepartment(cid,department);
    }

    public void updateDepartment(Department department)
    {
        departmentDAO.updateDepartment(department);
    }

    public void deleteDepartment(int depid)
    {
        departmentDAO.deleteDepartmnet(depid);
    }
}
